#!/bin/bash
#===============================================================================
# run.sh
#
# This script automates the execution of all analysis scripts to replicate the
# results presented in the manuscript. All output is logged to run.log.
#
# Usage:
#   bash run.sh
#===============================================================================

#-------------------------------------------------------------------------------
# 1. Settings: Stop on first error (-e) and echo commands (-x)
#-------------------------------------------------------------------------------
set -ex

#-------------------------------------------------------------------------------
# 2. Redirect ALL output (stdout & stderr) to run.log
#-------------------------------------------------------------------------------
exec > >(tee -a run.log) 2>&1

#-------------------------------------------------------------------------------
# 3. Print System Information
#-------------------------------------------------------------------------------
echo "========================================"
echo "System Information"
echo "----------------------------------------"
echo "Operating System:"
uname -a

echo -e "\nPython Version:"
python3 --version

echo -e "\nR Version:"
R --version

echo "========================================"
echo "Starting replication run at: $(date)"
echo "========================================"

#-------------------------------------------------------------------------------
# 4. Execute All Scripts in Order
#-------------------------------------------------------------------------------

--- Python Scripts ---
echo "Executing 00_get_raw_data.py..."
python3 00_get_raw_data.py

echo "Executing 01_write_and_sample_parquets.py..."
python3 01_write_and_sample_parquets.py

echo "Executing 02_preprocess_newspapers.py..."
python3 02_preprocess_newspapers.py

#--- R Scripts ---
echo "Executing 03_prep_cos_sim_crit.R..."
Rscript 03_prep_cos_sim_crit.R

echo "Executing 04_embed_combined.R..."
Rscript 04_embed_combined.R

echo "Executing 05_get_gsubbed_corpora.R..."
Rscript 05_get_gsubbed_corpora.R

echo "Executing 06_ts_cos_sims_leaders.R..."
Rscript 06_ts_cos_sims_leaders.R

echo "Executing 07_plot_cos_sims_leaders.R..."
Rscript 07_plot_cos_sims_leaders.R

echo "Executing 08_ts_cos_sims_exec.R..."
Rscript 08_ts_cos_sims_exec.R

echo "Executing 09_plot_cos_sims_exec.R..."
Rscript 09_plot_cos_sims_exec.R

echo "Executing 10_plot_combined.R"
Rscript 10_plot_combined.R

echo "Executing 11_get_synonyms.py"
python 11_get_synonyms.py

echo "Executing 12_ts_cos_sims_leaders_expanded.R..."
Rscript 12_ts_cos_sims_leaders_expanded.R

echo "Executing 13_plot_cos_sims_leaders_expanded.R..."
Rscript 13_plot_cos_sims_leaders_expanded.R

echo "Executing 14_ts_cos_sims_leaders_ip.R..."
Rscript 14_ts_cos_sims_leaders_ip.R

echo "Executing 15_plot_cos_sims_leaders_ip.R..."
Rscript 15_plot_cos_sims_leaders_ip.R

echo "Executing 16_ts_cos_sims_leaders_vary_transform.R..."
Rscript 16_ts_cos_sims_leaders_vary_transform.R

echo "Executing 17_plot_cos_sims_leaders_vary_transform.R..."
Rscript 17_plot_cos_sims_leaders_vary_transform.R

echo "Executing 18_ts_cos_sims_leaders_vary_feats.R..."
Rscript 18_ts_cos_sims_leaders_vary_feats.R

echo "Executing 19_plot_cos_sims_leaders_vary_feats.R..."
Rscript 19_plot_cos_sims_leaders_vary_feats.R

echo "Executing 20_ts_cos_sims_leaders_vary_targets.R..."
Rscript 20_ts_cos_sims_leaders_vary_targets.R

echo "Executing 21_plot_cos_sims_leaders_vary_targets.R..."
Rscript 21_plot_cos_sims_leaders_vary_targets.R

echo "Executing 22_ts_cos_sims_leaders_bysource_mastun.R..."
Rscript 22_ts_cos_sims_leaders_bysource_mastun.R

echo "Executing 23_synth_did_estimate.R..."
Rscript 23_synth_did_estimate.R

echo "Executing 24_calculate_changepoints.R..."
Rscript 24_calculate_changepoints.R

echo "Executing 31_combined_masress_labelled_forms.R..."
Rscript 31_combined_masress_labelled_forms.R

echo "Executing 32_plot_mastun_additional.R..."
Rscript 32_plot_mastun_additional.R

echo "Executing 41_bootstrap_main_results.R..."
Rscript 41_bootstrap_main_results.R

echo "Executing 42_plot_bootstrap_main_results.R..."
Rscript 42_plot_bootstrap_main_results.R

#-------------------------------------------------------------------------------
# 5. Final Message
#-------------------------------------------------------------------------------
echo "========================================"
echo "Replication run completed successfully at: $(date)"
echo "========================================"
